<?PHP

class Admin_wc_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','配送完成');
		$this->assign('menuid1','3'); 
		$this->assign('menuid2','3'); 
		//权限判断
		factory::module('admin')->authSys('complete'); 
	}
	
	/* 待生产配送批次 */
	public function index__(){	
	 	$this->assign('nav','待完成配送批次');
	}
	/* 已生产配送批次 */
	public function complete__(){	
	 	$this->assign('nav','已完成配送批次');
	}
	
		
	public function sc_list__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['status']))    $_POST['status']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			if(!isset($_POST['statusin']))    $_POST['statusin']  = ''; 
			 
  	  #查询条件
			$sql_where = ' where a.deleted=0 and a.status in ('.$_POST['statusin'].') '.func::speedsearch($_POST['keywords'],array( 'a.code' 	,'a.senddate' ));  
			
			if($_POST['b_date']!=''){
				$sql_where .= ' and a.senddate >= "'.$_POST['b_date'].'"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and a.senddate <= "'.$_POST['e_date'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_sendpi` a  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*
				from `t_sendpi` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
		exit();	
	}
	 
	public function daido__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$db = factory::getDBO();
			$db->begin(); 
			
			# 配送批次信息
			$rs_sendpi = $db->loadassoc('select a.* from `t_sendpi` a where a.deleted=0 and a.id="'.$_POST['id'].'" ');
			if(!$rs_sendpi){
				exit('没有查到任何记录！');
			}
			if($rs_sendpi['status']!='1'){
				exit('操作失败，当前配送批次状态有变化，请重新刷新界面再操作！');
			}
			//debug::show($rs_sendpi);
			if($rs_sendpi['onsource'] == '1'){
				$rs_source = $db->loadassoc('select * from t_source where sendpi_id="'.$rs_sendpi['id'].'"   ');
				if($rs_source['state'] != '1'){
					exit('操作失败，当前配送批次的溯源信息未录入完整！请提醒相关操作员！');
				}
			}
			
			
			
			# 检查是否包含溯源信息，并且溯源信息是否补充完毕，完毕之后才能进行完成操作
			
			
			
			//主表
			if(!$db->update('sendpi',array(
				'id'=>$_POST['id'],
				'status'=>'2',
				'updated'=>'now()',
				'updated_user_id'=>factory::module('admin')->getuserid()
			))){
				exit('操作失败！');
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
	public function daiundo__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$db = factory::getDBO();
			$db->begin(); 
			
			# 配送批次信息
			$rs_sendpi = $db->loadassoc('select a.* from `t_sendpi` a where a.deleted=0 and a.id="'.$_POST['id'].'" ');
			if(!$rs_sendpi){
				exit('没有查到任何记录！');
			}
			if($rs_sendpi['status']!='2'){
				exit('操作失败，当前配送批次状态有变化，请重新刷新界面再操作！');
			}
			
			//主表
			if(!$db->update('sendpi',array(
				'id'=>$_POST['id'],
				'status'=>'1',
				'updated'=>'now()',
				'updated_user_id'=>factory::module('admin')->getuserid()
			))){
				exit('操作失败！');
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
	

 
 
}
?>